import picUploadModule from "../components/picUploadModule";
import treeMenu from "../components/treeMenu";
import confirmBtn from "../components/confirmBtnModule";
import buryFormModule from "../components/buryFormModule";
import drawImageModule from "../components/drawImageModule";
import menuDropdown from "../components/menuDropdown";
import popover from "../components/popover";
import testImgUrl from "../assets/img/test01.jpg";
import noImgUrl from "../assets/img/no-img.png";
import eventApi from "./api/eventApi";
import requirementApi from "./api/requirementApi";
import pageApi from "./api/pageApi";
import applicationApi from "./api/application";
import processApi from "./api/processApi";
import analysisApi from "./api/analysisApi";
// import demo_data from "./mock/demo";
import moment from "moment";
let global = {};
let requirementStatusObj = {
	0: "设计中",
	// 1: "部门审核中",
	2: "实施中",
	// 3: "测试中",
	4: "待上线",
	5: "已上线",
	// 6: "大数据审核中"
};
let requirementTypeObj = {
	1: "新增",
	2: "修改",
	3: "删除"
};
let sdkTypeObj = {
	0: "IOS",
	1: "android",
	2: "Web",
	3: "微信小程序",
};
let pageTypeObj = {
	1: "页面",
	2: "区块",
	3: "展位"
};
let eventDataStatus = {
	0: "未采集",
	1: "已采集"
};
let appTypeObj = {
	1: "客户端",
	2: "web",
	3: "小程序",
}

export function compressImg(img, type, mx, mh) {
	return new Promise((resolve) => {
		const canvas = document.createElement('canvas');
		const context = canvas.getContext('2d');
		const {width: originWidth, height: originHeight} = img;
		const maxWidth = mx;
		const maxHeight = mh;
		let targetWidth = originWidth;
		let targetHeight = originHeight;
		if (originWidth > maxWidth || originHeight > maxHeight) {
			if (originWidth / originHeight > 1) {
				targetWidth = maxWidth;
				targetHeight = Math.round(maxWidth * (originHeight / originWidth));
			} else {
				targetHeight = maxHeight;
				targetWidth = Math.round(maxHeight * (originWidth / originHeight));
			}
		}
		canvas.width = targetWidth;
		canvas.height = targetHeight;
		context.clearRect(0, 0, targetWidth, targetHeight);
		context.drawImage(img, 0, 0, targetWidth, targetHeight);
		let url = canvas.toDataURL('image/png');
		resolve(url);
	});
}
/*
	dateCompare: 时间date区间对比
	入参：fromDate数组；compareDate数组
	出参：obj = {
		fromDate: [],
		compareDate: []
	}
*/
function dateCompare(fromDate, compareDate) {
	let len1, len2, arr, obj;
	len1 = moment(fromDate[1]).diff(moment(fromDate[0]), "days");
	len2 = moment(compareDate[1]).diff(moment(compareDate[0]), "days");
	if (len1 > len2) {
		arr = [compareDate[0], moment(compareDate[0]).add(len1, "days").format("YYYY-MM-DD")];
		obj = {
			fromDate: fromDate,
			compareDate: arr
		};
	} else {
		arr = [fromDate[0], moment(fromDate[0]).add(len2, "days").format("YYYY-MM-DD")];
		obj = {
			fromDate: arr,
			compareDate: compareDate
		};
	}
	return obj;
}

global.install = function (Vue) {
	Vue.prototype.page = {
		current: 1,
		pageSize: 10,
		pageSizes: [10, 20, 30, 40]
	}
	Vue.prototype.dataToTree = function (data) {
		let treeData = [], map = {};
		if (!Array.isArray(data)) {
			return treeData;
		}
		data.forEach(item => {
			delete item.children;
			map[item.id] = item;
		});
		data.forEach(item => {
			let parent = map[item.parentId];
			if (parent) {
				(parent.children || (parent.children = [])).push(item);
			} else {
				treeData.push(item);
			}
		})
		return treeData;
	}
	Vue.prototype.$handleRequirementType = function (requirementType) {
		let type = "";
		if (requirementType === 1) {
			type = "新增";
		} else if (requirementType === 2) {
			type = "修改"
		} else if (requirementType === 3 || (requirementType === 4)) {
			type = "删除"
		}
		return type;
	}
	Vue.prototype.$requirementStatusObj = requirementStatusObj;
	Vue.prototype.$requirementStatusList = (
		function () {
			let list = [];
			for (let i in requirementStatusObj) {
				list.push({
					value: Number(i),
					label: requirementStatusObj[i]
				})
			}
			return list;
		}
	)();
	Vue.prototype.$sdkTypeObj = sdkTypeObj;
	Vue.prototype.$sdkTypeList = (
		function () {
			let list = [];
			for (let i in sdkTypeObj) {
				list.push({
					value: Number(i),
					label: sdkTypeObj[i]
				})
			}
			return list;
		}
	)();
	Vue.prototype.$pageTypeObj = pageTypeObj;
	Vue.prototype.$pageTypeList = (
		function () {
			let list = [];
			for (let i in pageTypeObj) {
				list.push({
					value: Number(i),
					label: pageTypeObj[i]
				})
			}
			return list;
		}
	)();
	Vue.prototype.$productCodeObj = {
		"wz": "网舟",
		"jg": "极光",
		"dx": "电信"
	}
	Vue.prototype.$productCodeList = [
		{
			value: "wz",
			label: "网舟"
		},
		{
			value: "jg",
			label: "极光"
		},
		{
			value: "dx",
			label: "电信"
		},
	];

	Vue.prototype.$requirementTypeObj = requirementTypeObj;
	Vue.prototype.$appTypeObj = appTypeObj;
	Vue.prototype.$eventDataStatus = eventDataStatus;
	Vue.prototype.$testImageUrl = testImgUrl;
	Vue.prototype.$noImgUrl = noImgUrl;
	Vue.prototype.$compressImg = compressImg;
	Vue.prototype.$eventApi = eventApi;
	Vue.prototype.$requirementApi = requirementApi;
	Vue.prototype.$pageApi = pageApi;
	Vue.prototype.$applicationApi = applicationApi;
	Vue.prototype.$applicationApi = applicationApi;
	Vue.prototype.$processApi = processApi;
	Vue.prototype.$analysisApi = analysisApi;
	Vue.prototype.$dateCompare = dateCompare;
	Vue.component("picUploadModule", picUploadModule);
	Vue.component("treeMenu", treeMenu);
	Vue.component("confirmBtn", confirmBtn);
	Vue.component("buryFormModule", buryFormModule);
	Vue.component("drawImageModule", drawImageModule);
	Vue.component("menuDropdown", menuDropdown);
	Vue.component("popover", popover);
};
export default global;
